package date_;

/**
 * @author wly
 * @version V1.0.0
 * @date 2022/12/24 14:24
 */
public class Homework02 {
    public static void main(String[] args) {
//        System.out.println(isDigital("016023"));
        String res = null;
        try {
            res = judge("hi", "320968", "82734ge7@qq.com");
            System.out.println(res);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static String judge(String userName, String passWord, String email){
        if (userName== null){
            throw new RuntimeException("用户名不能为空！");
        }
        if (passWord== null){
            throw new RuntimeException("密码不能为空！");
        }
        if (email== null){
            throw new RuntimeException("邮箱不能为空！");
        }
        int userNameLength = userName.length();
        if (!(userNameLength == 2 || userNameLength == 3 || userNameLength == 4)){
            throw new RuntimeException("用户名长度必须为2或3或3！");
        }
        int passWordLength = passWord.length();
        if (!(passWordLength == 6)){
            throw new RuntimeException("密码长度必须为6！");
        }
        if (isDigital(passWord) == false){
            throw new RuntimeException("密码必须全是数字！");
        }
        judgeEmail(email);
        return "信息录入成功！";
    }

    public static boolean isDigital(String str){
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if (!(aChar >= 48 && aChar <= 57)){
                return false;
            }
        }
        return true;
    }

    public static boolean judgeEmail(String str){
        if (!(str.contains("@") && str.contains("."))){
            throw new RuntimeException("邮箱中必须包含@和.！");
        }
        int index1 = str.indexOf('@');
        int index2 = str.indexOf('.');
        if (index1 >= index2){
            throw new RuntimeException("@必须在.的前面！");
        }
        return true;
    }
}
